<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('sp_class', LIB_PATH);

$main = new Sp($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$self = isset($_POST['self']) ? trim($_POST['self']) : trim($_GET['self']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;

switch ($cf_action) {
    case "index":
	$smarty->mypage($cf_controller, $cf_action, 0);

	//echo get_low_price(0,0,5);
	//exit;	
	$stype_ary = add_array_item(array('' => '全部'), $cf_info_var_common['sp_contract_type']);
	$stype = isset($_POST['stype']) ? trim($_POST['stype']) : urldecode($_GET['stype']);
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode($_GET['skey2']);
	$skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode($_GET['skey3']);
    $skey4 = isset($_POST['skey4']) ? trim($_POST['skey4']) : urldecode($_GET['skey4']);
    $skey5 = isset($_POST['skey5']) ? trim($_POST['skey5']) : urldecode($_GET['skey5']);
    $skey6 = isset($_POST['skey6']) ? trim($_POST['skey6']) : urldecode($_GET['skey6']);
	$skey7 = isset($_POST['skey7']) ? trim($_POST['skey7']) : urldecode($_GET['skey7']);
	$skey8 = isset($_POST['skey8']) ? trim($_POST['skey8']) : urldecode($_GET['skey8']);
	$skey9 = isset($_POST['skey9']) ? trim($_POST['skey9']) : urldecode($_GET['skey9']);
	$order = trim($_POST['order']) ? trim($_POST['order']) : trim($_GET['order']);
	$where = "`sp_check`='1' and sp_status='1'";
	

	if ($skey) {
	    $where .= " and sp_title like '%" . trim($skey) . "%'";
	}
	if ($skey2) {
	    $where .= " and sp_name = '" . $skey2 . "'";
	}
	if ($skey3) {
	    $where .= " and sp_contact = '" . $skey3 . "'";
	}
	if ($skey4) {
	    $where .= " and sp_phone = '" . $skey2 . "'";
	}

	
	if ($skey5) {
		$where .= " and (sp_last_rebate < '" . $skey5 . "' or sp_last_rebate is null )";
    }
	
	
	if ($stype) {
		$where .= " and sp_last_rebate >''";
	}
	switch($order){
			case "money":
				$key_order = "sp_money desc";
			break;	
			default:
				$key_order ="sp_id DESC";	
			break;
		}
	
	$key_word= '?dpt=' . $cf_dpt . '&self=' . $self. '&skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3. '&skey4=' . $skey4 . '&skey5=' . $skey5 . '&skey6=' . $skey6 . '&stype=' . $stype . '&stype2=' . $stype2;
	
	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, $key_order, intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page.$key_word.'&order='.$order );
	//echo $MyPage->get_sql();
	
	break;
    case "withdraw":
	if ($cf_ispost) {

		$db->begin();
		$main = new Sp($db);

			
		$row = $main->get_data_row($id);
		$money = $_POST['money'];
		$result = $row['sp_money']-$money;
		
		if($row['sp_bank_type'] && $row['sp_bank_name'] && $row['sp_bank_code'] ){
			
		}else{
			my_jump('代理商的银行信息不完善');
		}
		
		if($result>=0 && $money>0){
			
		}else{
			my_jump('输入的金额有误');
		}
		
	    //$db->debug(1,1);
	    $data = array(
			'sp_money'=>"sp_money-$money",
			'sp_last_rebate'=>date("Y-m-d H:i:s"),
			'sp_last_rebate_admin'=>$cf_admin_info['admin_name'],
		);
		
		$rs1 = $main->update_data($data, "sp_id='$id'");
		$content = "原有:".$row['sp_money'] .',支出：'.(0-$money). ",剩余:".$result; 
		$remark = '结账类型：'.$row['sp_bank_type']. ' 姓名:'.$row['sp_bank_name']. ' 账号:'.$row['sp_bank_code'];
		
		$sp_money_data = array(
					  'log_spid' => $row['sp_id'],
					  'log_sp' => $row['sp_name'],
					  'log_type' => 'withdraw',
					  'log_money' => 0-$money,
					  'log_content' => "结账 (".$content.")",
					  'log_create_time' => date("Y-m-d H:i:s"),
					  'log_main_id' => $row['sp_id'],
					  'log_remark' => $remark,
					  'log_money_first' => $row['sp_money'],
					  'log_money_end' => $result,
					  'log_bank_type' => $row['sp_bank_type'],
					  'log_bank_name' => $row['sp_bank_name'],
					  'log_bank_code' => $row['sp_bank_code'],
					  'log_admin' => $cf_admin_info['admin_name'],
					  
				);
		$rs2 = $db->insert('data_sp_money_log', $sp_money_data); //商家资金记录	

	    if ($rs1 && $rs2) {
			$db->commit();
			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			unset($_SESSION['pre_url']);
			my_jump('提交成功' , '', $cf_self_page."?act=withdraw&id=".$row['sp_id'].'&dpt='.$cf_dpt, 3);
	    } else {
			$db->back();	
			my_jump('提交失败');
	    }
	} else {
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		$row = $main->get_data_row($id);
	    if (!$row)
		my_jump('沒有此信息');
		
		load_file('log_class', LIB_PATH);
		$log = new Log($db);
		$data = $log->get_sp_log('money',"log_spid='$id' and log_type='withdraw'");
	   //dump($data);
	}

	break;
   
    default:
	my_jump('非法操作!');
	break;
}

$smarty->assign(array(
    'id' => $id,
    'row' => htmlchar($row),
    'data' => $data,
	'data_ary' => $data_ary,
    'page' => $page,
    'self' => $self,
    'skey' => $skey,
    'skey2' => $skey2,


));


$smarty->mydisplay();
?>